package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findRoleByUserId(String id) {
        return roleDao.findRoleByUserId(id);
    }

    @Override
    public void updateRoleModule(String roleid, String moduleIds) {
        //1.根据roleid从pe_role_module表中删除关联数据
        roleDao.deleteModuleByRoleId(roleid);

        //2.对moduleIds进行处理
        String[] ids = moduleIds.split(",");
        for(String moduleId:ids){
            roleDao.updateRoleModule(roleid,moduleId); //重新建立关系
        }

    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public void delete(String id) {
        roleDao.delete(id);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public PageInfo<Role> findAll(String companyId, int page, int size) {
        PageHelper.startPage(page,size);
        List<Role> all = roleDao.findAll(companyId);
        return new PageInfo<>(all);
    }
}
